from django.http import HttpResponse
import json
from django.shortcuts import render
from django.views import View


def index(request):
    return HttpResponse('hello world')


def get_querystring(request):
    data = request.GET
    return HttpResponse(data.get('name'))


def post_querydict(request):
    data = request.POST
    name = data.get('name')
    print(name)
    return HttpResponse(name)


def post_json(request):
    data = request.body
    data = data.decode()
    json_data = json.loads(data)
    print(json_data['name'])
    return HttpResponse('hahaha')


def set_cookie(request):
    response = HttpResponse()
    response.set_cookie('name', 'Sam')
    response.content = '设置cookie'
    return response


def get_cookie(request):
    cookie = request.COOKIES
    name = cookie.get('name')
    print(name)
    return HttpResponse(name)


def set_session(request):
    request.session['name'] = 'Tom'
    request.session['age'] = 18
    return HttpResponse('设置session')


def get_session(request):
    name = request.session.get('name')
    age = request.session.get('age')
    return HttpResponse('%s: %s' % (name, age))


def del_session(request):
    request.session.flush()
    return HttpResponse('删除session')


def render_templates(request):
    return render(request, '../templates/index.html', {
        'alist': ['a', 'b', 'c', 'd'],
    })


class RegisterView(View):

    def get(self,request):
        return HttpResponse('get')

    def post(self, request):
        return HttpResponse('post')

    def put(self, request):
        return HttpResponse('put')

    def delete(self, request):
        return HttpResponse('delete')



